#!/usr/bin/env bash

set -e

# Setup for semaphore ci

if [ "${CI}" != "true" -o "${SEMAPHORE}" != "true" ]; then
	echo "not on semaphoreci"
	exit 1
fi

# Install etcd
mkdir etcd
pushd etcd
curl -L https://github.com/coreos/etcd/releases/download/v3.2.11/etcd-v3.2.11-linux-amd64.tar.gz -o etcd-v3.2.11-linux-amd64.tar.gz
tar xzvf etcd-v3.2.11-linux-amd64.tar.gz
popd

# Install consul
mkdir consul
pushd consul
curl -L https://releases.hashicorp.com/consul/1.0.6/consul_1.0.6_linux_amd64.zip -o consul_1.0.6_linux_amd64.zip
unzip consul_1.0.6_linux_amd64.zip
popd

# Install postgreSQL 9.5, 9.6, 10 and 11
# TODO(sgotti) remove this when semaphoreci images will have this already installed
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main 10" > /etc/apt/sources.list.d/pgdg.list'
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main 11" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql-9.5 postgresql-9.6 postgresql-10 postgresql-11 eatmydata

# Fix build to work with the right import path also when building github forked repositories
if [[ ! -e ~/workspace/src/github.com/sorintlab/stolon ]]; then
  mkdir -p ~/workspace/src/github.com/sorintlab
  ln -s /home/runner/stolon ~/workspace/src/github.com/sorintlab/stolon
fi

# Run tests
export ETCD_BIN="${PWD}/etcd/etcd-v3.2.11-linux-amd64/etcd"
export CONSUL_BIN="${PWD}/consul/consul"

OLDPATH=$PATH

# Increase sysv ipc semaphores to accomode a big number of parallel postgres instances
sudo /bin/sh -c 'echo "32000 1024000000 500 32000" > /proc/sys/kernel/sem'

# Free up some disk space
rm -rf ~/.rbenv

sudo /etc/init.d/elasticsearch stop
sudo /etc/init.d/cassandra stop

export INTEGRATION=1
export PARALLEL=5

## Temporary hack disable checksum verification since semaphore has go 1.11.0 that had a bug causing different checksums than the right one generated by go 1.11.2
rm go.sum

LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+"$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata
LD_PRELOAD=${LD_PRELOAD:+"$LD_PRELOAD "}libeatmydata.so

# Test with postgresql 9.5
echo "===== Testing with postgreSQL 9.5 ====="
export PATH=/usr/lib/postgresql/9.5/bin/:$OLDPATH ; ./test

# Test with postgresql 9.6
echo "===== Testing with postgreSQL 9.6 ====="
export PATH=/usr/lib/postgresql/9.6/bin/:$OLDPATH ; ./test

# Test with postgresql 10
echo "===== Testing with postgreSQL 10 ====="
export PATH=/usr/lib/postgresql/10/bin/:$OLDPATH ; ./test

# Test with postgresql 11
echo "===== Testing with postgreSQL 11 ====="
export PATH=/usr/lib/postgresql/11/bin/:$OLDPATH ; ./test
